package top.xiaoshuaila.moments.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import top.xiaoshuaila.moments.pojo.Message;

import java.sql.Timestamp;
import java.util.List;

@Repository
public interface MessageMapper {
    @Select("select \n" +
            "  a.id id,\n" +
            "  b.headphoto headphoto,\n" +
            "  a.manid manid,\n" +
            "  a.mess mess,\n" +
            "  a.type type,\n" +
            "  a.time time,\n" +
            "  a.isread isread,\n" +
            "  a.dyid dyid \n" +
            "from\n" +
            "  message a \n" +
            "  left join users b \n" +
            "    on a.manid = b.id \n" +
            "where a.userid = ${id}  order by a.time desc limit ${page},${num}")
    List<Message> getMessages(long id,int page,int num);
    @Update("update message set message.isread=1 where message.userid=${id}")
    Integer clearMessages(long id);
    @Delete("delete from message where message.type=0 and message.manid=${manid} and message.dyid=${dyid}")
    void deleteMessageszan(long dyid, long manid);
    @Delete("delete from message where message.type=1 and message.manid=${id} and message.userid=${vid}")
    void deleteMessagesfocu(long id, long vid);
    @Insert("insert into message (type,dyid,userid,mess,time,manid)values (${type},${dyid},${userid},'${mess}','${time}',${manid})")
    void addMessageszan(long type, long dyid, long userid, String mess, Timestamp time,
                     long manid);
    @Insert("insert into message (type,userid,mess,time,manid)values (${type},${userid},'${mess}','${time}',${manid})")
    void addMessagesfocu(long type,long userid, String mess,Timestamp time,long manid);
    @Select("select count(*)" +
            "from\n" +
            "  message a \n" +
            "  left join users b \n" +
            "    on a.manid = b.id \n" +
            "where a.userid = ${id}")
    int gettotal(long id);
}
